package com.fz.util;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fz.controller.TUserController;
import com.fz.entity.TUser;
import com.fz.service.impl.TUserServiceImpl;
import com.fz.vo.ResultVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.math.BigDecimal;

import static org.junit.Assert.assertNotNull;

@RunWith(SpringRunner.class)
@SpringBootTest
public class testemail {
    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    public void contextLoads() {
        /*一封简单的邮件*/
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        /*邮件标题*/
        mailMessage.setSubject("邮件验证码");
        /*邮件内容(随机生成验证码)*/
        String code = UUID.randomUUID().toString().substring(0,4);
        mailMessage.setText(code);
        /*发送人(必须和配置文件中的username相同)*/
        mailMessage.setFrom("3175519761@qq.com");
        /*收件人*/
        mailMessage.setTo("2096635386@qq.com");
        /*发送*/
        mailSender.send(mailMessage);
    }
    @Test
    public void test(){
        System.out.println(FzUtil.randomOrderCode("TF"));
    }



    @Autowired
    TUserServiceImpl tUserService;

    @Autowired
    TUserController tUserController;

    // 测试增加用户
    @Test
    public void testAddUser() {
        // 创建测试用户
        String phone = "18328212151";
        String password = "123456";
        String email = "test@example.com";
        String role = "user";


//        System.out.println("TUserServiceImpl: " + tUserService);
//        System.out.println("TUserController: " + tUserController);
//
//        ResultVo<Boolean> result = tUserController.register(phone,password,email,role);
//        System.out.println("注册结果："+result);
//        assertNotNull(result);
//        // 创建用户对象
//        TUser newUser = new TUser(phone, password, email, role);
//
//        // 保存用户
//        boolean result = tUserService.save(newUser);
//        System.out.println("新增用户结果: " + (result ? "成功" : "失败"));
//
//        // 验证是否添加成功
//        TUser savedUser = tUserService.getOne(
//                new QueryWrapper<TUser>().eq("phone", phone)
//        );
//        Assert.notNull(savedUser, "用户应成功创建");
//        System.out.println("新增用户ID: " + savedUser.getId());

        TUser user=tUserService.login(phone,password);

        System.out.println(user.getNickName()+"\n"+user.getPassword()+"\n"+user.getUsername()+"\n"+user.getPhone());

    }

    // 测试更新用户信息
    @Test
    public void testUpdateUser() {
        // 获取测试用户 (假设ID为1的用户存在)
        Long userId = 1L;
        TUser user = tUserService.getById(userId);

        if (user != null) {
            // 修改用户信息
            String newNickname = "更新后的昵称_" + UUID.randomUUID().toString().substring(0, 4);
            user.setNickName(newNickname);

            // 执行更新
            boolean result = tUserService.updateById(user);
            System.out.println("更新用户结果: " + (result ? "成功" : "失败"));

            // 验证更新结果
            TUser updatedUser = tUserService.getById(userId);
            Assert.isTrue(newNickname.equals(updatedUser.getNickName()),
                    "昵称应更新为: " + newNickname);
        } else {
            System.out.println("测试用户不存在，跳过更新测试");
        }
    }

    // 测试禁用用户（逻辑删除）
    @Test
    public void testDisableUser() {
        // 创建测试用户
        TUser testUser = createTestUser();
        Long userId = testUser.getId();

        // 禁用用户
        boolean result = tUserService.del(userId, false);
        System.out.println("禁用用户结果: " + (result ? "成功" : "失败"));

        // 验证禁用状态
        TUser disabledUser = tUserService.getById(userId);
        Assert.isTrue(!disabledUser.getStatus(), "用户状态应为禁用");
    }

    // 测试物理删除用户
    @Test
    public void testDeleteUser() {
        // 创建测试用户
        TUser testUser = createTestUser();
        Long userId = testUser.getId();

        // 物理删除用户
        boolean result = tUserService.removeById(userId);
        System.out.println("删除用户结果: " + (result ? "成功" : "失败"));

        // 验证是否已删除
        TUser deletedUser = tUserService.getById(userId);
        Assert.isNull(deletedUser, "用户应已被删除");
    }

    // 测试充值功能
    @Test
    public void testRecharge() {
        // 获取测试用户 (假设ID为2的用户存在)
        Long userId = 2L;
        TUser user = tUserService.getById(userId);

        if (user != null) {
            BigDecimal originalBalance = user.getAccount();
            BigDecimal rechargeAmount = new BigDecimal("100.00");

            // 执行充值
            boolean result = tUserService.recharge(userId, rechargeAmount);
            System.out.println("充值结果: " + (result ? "成功" : "失败"));

            // 验证余额
            TUser updatedUser = tUserService.getById(userId);
            BigDecimal expectedBalance = originalBalance.add(rechargeAmount);
            Assert.isTrue(expectedBalance.compareTo(updatedUser.getAccount()) == 0,
                    "余额应增加100元");
        } else {
            System.out.println("测试用户不存在，跳过充值测试");
        }
    }

    // 辅助方法：创建测试用户
    private TUser createTestUser() {
        String phone = "138" + UUID.randomUUID().toString().substring(0, 8);
        TUser testUser = new TUser(
                phone,
                "Temp@123",
                "temp_" + phone + "@test.com",
                "test"
        );
        tUserService.save(testUser);
        return testUser;
    }
}
